package com.qimu.saiyou.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qimu.saiyou.annotation.AuthCheck;
import com.qimu.saiyou.common.BaseResponse;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.common.ResultUtils;
import com.qimu.saiyou.constant.UserConstant;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.model.dto.tag.TagAddRequest;
import com.qimu.saiyou.model.dto.tag.TagQueryRequest;
import com.qimu.saiyou.model.entity.Tag;
import com.qimu.saiyou.service.TagService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author: FeiXiang
 * @Date: 2023年04月11日 11:37
 * @Version: 1.0
 * @Description: 标签
 */
@RestController
@RequestMapping("/tag")
public class TagController {
    @Resource
    private TagService tagService;

    @PostMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Tag>> listTag(@RequestBody TagQueryRequest tagQueryRequest) {
        String searchText = tagQueryRequest.getSearchText();
        Integer status = tagQueryRequest.getStatus();
        int current = tagQueryRequest.getCurrent();
        int pageSize = tagQueryRequest.getPageSize();

        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(searchText), Tag::getTagName, searchText);
        wrapper.eq(ObjectUtils.isNotEmpty(status), Tag::getStatus, status);
        Page<Tag> tagPage = tagService.page(new Page<>(current, pageSize), wrapper);
        return ResultUtils.success(tagPage);
    }

    @GetMapping("/list/vo")
    public BaseResponse<List<Tag>> listVoTag() {
        List<Tag> tags = tagService.lambdaQuery().eq(Tag::getStatus, 0).list();
        return ResultUtils.success(tags);
    }

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> addTag(@RequestBody TagAddRequest tagAddRequest) {
        if (ObjectUtils.isEmpty(tagAddRequest) || CollUtil.isEmpty(tagAddRequest.getTags())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Tag> tagList = tagAddRequest.getTags().stream().map(t -> {
            Tag tag = new Tag();
            tag.setTagName(t);
            return tag;
        }).collect(Collectors.toList());
        boolean saveBatch = tagService.saveBatch(tagList);
        return ResultUtils.success(saveBatch);
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateTag(@RequestBody Tag tag) {
        if (tag == null || tag.getId() == null || tag.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag oldTag = tagService.getById(tag);
        if (oldTag == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        if (tag.getTagName() != null) {
            oldTag.setTagName(tag.getTagName());
        }
        return ResultUtils.success(tagService.updateById(oldTag));
    }

    @GetMapping("/offline")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> offlineTag(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag oldTag = tagService.getById(id);
        if (oldTag == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        oldTag.setStatus(1);
        return ResultUtils.success(tagService.updateById(oldTag));
    }

    @GetMapping("/online")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> onlineTag(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag oldTag = tagService.getById(id);
        if (oldTag == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        oldTag.setStatus(0);
        return ResultUtils.success(tagService.updateById(oldTag));
    }
    @GetMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteTag(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag oldTag = tagService.getById(id);
        if (oldTag == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(tagService.removeById(oldTag));
    }
}
